package com.java.example.ui.dialog

import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.WindowManager
import android.widget.Button
import com.blankj.utilcode.util.SizeUtils
import com.java.example.R

class SettingDialog {
    private lateinit var mDialog: AlertDialog
    fun showDialog(context: Context?, onCommit: (Context) -> Unit) {
        mDialog = AlertDialog.Builder(context).create()
        mDialog.show()
        mDialog.window?.let {
            it.setContentView(R.layout.user_setting)
            it.setGravity(Gravity.END)
            it.setWindowAnimations(R.style.anim_panel_up_from_right)
            it.decorView.setPadding(0, 0, 0, 0)
            it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            //设置属性
            val params = it.attributes
            params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
            params.height = WindowManager.LayoutParams.MATCH_PARENT
            params.width = SizeUtils.dp2px(240f)
            params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
            params.dimAmount = 0.5f
            it.attributes = params

            it.findViewById<Button>(R.id.btn_logout).apply {
                setOnClickListener {
                    mDialog.dismiss()
                    onCommit.invoke(context!!)
                }
            }
        }
    }

    companion object {
        fun create(): SettingDialog {
            return SettingDialog()
        }
    }
}
